package com.sc.smartcommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.smartcommunity.domain.Moment;
import com.sc.smartcommunity.domain.MomentComment;
import com.sc.smartcommunity.mapper.MomentCommentMapper;
import com.sc.smartcommunity.mapper.MomentMapper;
import com.sc.smartcommunity.service.MomentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Chenke Zhao
 * @date 2025/9/15 16:28
 * @description MomentServiceImpl
 */
@Service
public class MomentServiceImpl extends ServiceImpl<MomentMapper, Moment> implements MomentService {

    @Resource
    private MomentCommentMapper momentCommentMapper;


    @Override
    public void like(Long id) {
        Moment moment = getById(id);
        moment.setLikeCount(moment.getLikeCount() + 1);
        updateById(moment);
    }

    @Override
    public void unlike(Long id) {
        Moment moment = getById(id);
        moment.setLikeCount(moment.getLikeCount() - 1);
        updateById(moment);
    }

    @Override
    public void comment(MomentComment momentComment) {
        Moment moment = getById(momentComment.getMomentId());
        moment.setCommentCount(moment.getCommentCount() + 1);
        updateById(moment);
        momentCommentMapper.insert(momentComment);
    }

    @Override
    public void deleteComment(Long id) {
        momentCommentMapper.deleteById(id);
    }


    @Override
    public List<MomentComment> queryCommentList(Integer momentId) {
        return momentCommentMapper.selectList(new LambdaQueryWrapper<MomentComment>().eq(MomentComment::getMomentId, momentId));
    }
}
